﻿using System;
using System.Data;
using System.Collections.Generic;
using HsTec.Mobile.Green.Model;
using HsTec.Mobile.Green.Util;
using System.Data.SqlClient;
using System.Text;
namespace HsTec.Mobile.Green.BLL
{
    /// <summary>
    /// 业务逻辑类TotalProject 的摘要说明。
    /// </summary>
    public class TotalProjectBLL
    {
        private readonly HsTec.Mobile.Green.DAL.TotalProjectDAL dal = new HsTec.Mobile.Green.DAL.TotalProjectDAL();
        public TotalProjectBLL()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sWhere)
        {
            return dal.Exists(sWhere);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.TotalProject model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(HsTec.Mobile.Green.Model.TotalProject model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string tp_ids)
        {

            return dal.Delete(tp_ids);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.TotalProject GetModel(int tp_id)
        {

            return dal.GetModel(tp_id);
        }

        public HsTec.Mobile.Green.Model.TotalProjectEntity GetModelEntity(int tp_id)
        {

            return dal.GetModelEntity(tp_id);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<HsTec.Mobile.Green.Model.TotalProject> GetModelList(string strWhere)
        {
            DataView ds = dal.GetList(strWhere);
            return DataTableToList(ds.Table);
        }
        public List<HsTec.Mobile.Green.Model.TotalProjectEntity> GetModelListEntity(string strWhere)
        {
            DataView ds = dal.GetListEntity(strWhere);
            return DataTableToListEntity(ds.Table);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<HsTec.Mobile.Green.Model.TotalProject> DataTableToList(DataTable dt)
        {
            List<HsTec.Mobile.Green.Model.TotalProject> modelList = new List<HsTec.Mobile.Green.Model.TotalProject>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                HsTec.Mobile.Green.Model.TotalProject model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new HsTec.Mobile.Green.Model.TotalProject();
                    if (dt.Rows[n]["tp_id"].ToString() != "")
                    {
                        model.tp_id = int.Parse(dt.Rows[n]["tp_id"].ToString());
                    }
                    model.tasktype = dt.Rows[n]["tasktype"].ToString();
                    model.stage = dt.Rows[n]["stage"].ToString();
                    model.taskno = dt.Rows[n]["taskno"].ToString();
                    model.projectno = dt.Rows[n]["projectno"].ToString();
                    model.commpany = dt.Rows[n]["commpany"].ToString();
                    model.name = dt.Rows[n]["name"].ToString();
                    model.leaders = dt.Rows[n]["leaders"].ToString();
                    model.projecttype = dt.Rows[n]["projecttype"].ToString();
                    model.progresstype = dt.Rows[n]["progresstype"].ToString();
                    model.person1 = dt.Rows[n]["person1"].ToString();
                    model.person2 = dt.Rows[n]["person2"].ToString();
                    model.person3 = dt.Rows[n]["person3"].ToString();
                    model.planner = dt.Rows[n]["planner"].ToString();
                    if (dt.Rows[n]["tpdate"].ToString() != "")
                    {
                        model.tpdate = DateTime.Parse(dt.Rows[n]["tpdate"].ToString());
                    }
                    model.business = dt.Rows[n]["business"].ToString();
                    model.comments1 = dt.Rows[n]["comments1"].ToString();
                    model.comments2 = dt.Rows[n]["comments2"].ToString();
                    model.reviewedchange = dt.Rows[n]["reviewedchange"].ToString();
                    model.meetingchange = dt.Rows[n]["meetingchange"].ToString();
                    model.remark = dt.Rows[n]["remark"].ToString();
                    model.cooperationunit = dt.Rows[n]["cooperationunit"].ToString();
                    model.cooperationperson = dt.Rows[n]["cooperationperson"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        public List<HsTec.Mobile.Green.Model.TotalProjectEntity> DataTableToListEntity(DataTable dt)
        {
            List<HsTec.Mobile.Green.Model.TotalProjectEntity> modelList = new List<HsTec.Mobile.Green.Model.TotalProjectEntity>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                HsTec.Mobile.Green.Model.TotalProjectEntity model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new HsTec.Mobile.Green.Model.TotalProjectEntity();
                    if (dt.Rows[n]["tp_id"].ToString() != "")
                    {
                        model.tp_id = int.Parse(dt.Rows[n]["tp_id"].ToString());
                    }
                    model.tasktype = dt.Rows[n]["tasktype"].ToString();
                    model.stage = dt.Rows[n]["stage"].ToString();
                    model.taskno = dt.Rows[n]["taskno"].ToString();
                    model.projectno = dt.Rows[n]["projectno"].ToString();
                    model.commpany = dt.Rows[n]["commpany"].ToString();
                    model.name = dt.Rows[n]["name"].ToString();
                    model.leaders = dt.Rows[n]["leaders"].ToString();
                    model.projecttype = dt.Rows[n]["projecttype"].ToString();
                    model.progresstype = dt.Rows[n]["progresstype"].ToString();
                    model.person1 = dt.Rows[n]["person1"].ToString();
                    model.person2 = dt.Rows[n]["person2"].ToString();
                    model.person3 = dt.Rows[n]["person3"].ToString();
                    model.planner = dt.Rows[n]["planner"].ToString();
                    if (dt.Rows[n]["tpdate"].ToString() != "")
                    {
                        model.tpdate = DateTime.Parse(dt.Rows[n]["tpdate"].ToString());
                    }
                    model.business = dt.Rows[n]["business"].ToString();
                    model.comments1 = dt.Rows[n]["comments1"].ToString();
                    model.comments2 = dt.Rows[n]["comments2"].ToString();
                    model.reviewedchange = dt.Rows[n]["reviewedchange"].ToString();
                    model.meetingchange = dt.Rows[n]["meetingchange"].ToString();
                    model.remark = dt.Rows[n]["remark"].ToString();
                    model.cooperationunit = dt.Rows[n]["cooperationunit"].ToString();
                    model.cooperationperson = dt.Rows[n]["cooperationperson"].ToString();

                    model.personname1 = dt.Rows[n]["personname1"].ToString();
                    model.personname2 = dt.Rows[n]["personname2"].ToString();
                    model.personname3 = dt.Rows[n]["personname3"].ToString();
                    model.plannername = dt.Rows[n]["plannername"].ToString();
                    model.businessname = dt.Rows[n]["businessname"].ToString();
                    model.leadersname = dt.Rows[n]["leadersname"].ToString();

                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  成员方法

        public int Add(TotalProject model, List<Commitment> list)
        {
            SQLHelper.myconn.Open();
            SqlCommand command = SQLHelper.myconn.CreateCommand();
            SqlTransaction transaction;
            transaction = SQLHelper.myconn.BeginTransaction();
            command.Connection = SQLHelper.myconn;
            command.Transaction = transaction;
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into pm_TotalProject(");
                strSql.Append("tasktype,stage,taskno,projectno,commpany,name,leaders,projecttype,progresstype,person1,person2,person3,planner,tpdate,business,comments1,comments2,reviewedchange,meetingchange,remark,cooperationunit,cooperationperson");
                strSql.Append(")");
                strSql.Append(" values (");
                strSql.Append("'" + model.tasktype + "',");
                strSql.Append("'" + model.stage + "',");
                strSql.Append("'" + model.taskno + "',");
                strSql.Append("'" + model.projectno + "',");
                strSql.Append("'" + model.commpany + "',");
                strSql.Append("'" + model.name + "',");
                strSql.Append("'" + model.leaders + "',");
                strSql.Append("'" + model.projecttype + "',");
                strSql.Append("'" + model.progresstype + "',");
                strSql.Append("'" + model.person1 + "',");
                strSql.Append("'" + model.person2 + "',");
                strSql.Append("'" + model.person3 + "',");
                strSql.Append("'" + model.planner + "',");
                strSql.Append("'" + model.tpdate + "',");
                strSql.Append("'" + model.business + "',");
                strSql.Append("'" + model.comments1 + "',");
                strSql.Append("'" + model.comments2 + "',");
                strSql.Append("'" + model.reviewedchange + "',");
                strSql.Append("'" + model.meetingchange + "',");
                strSql.Append("'" + model.remark + "',");
                strSql.Append("'" + model.cooperationunit + "',");
                strSql.Append("'" + model.cooperationperson + "'");
                strSql.Append(")");
                command.CommandText = strSql.ToString();
                command.ExecuteNonQuery();
                string tpid = SQLHelper.ExecuteString("select max(tp_id) from pm_TotalProject", SQLHelper.myconn, transaction);
                foreach (Commitment com in list)
                {
                    StringBuilder strSql1 = new StringBuilder();
                    com.tp_id = int.Parse(tpid);
                    strSql1.Append("insert into pm_tp_Commitment(");
                    strSql1.Append("professional,departments,completiondate,deliverydate,reviewer,branchreviewer,qualityplan,conferencereview,tp_id");
                    strSql1.Append(")");
                    strSql1.Append(" values (");
                    strSql1.Append("'" + com.professional + "',");
                    strSql1.Append("" + com.departments + ",");
                    strSql1.Append("'" + com.completiondate + "',");
                    strSql1.Append("'" + com.deliverydate + "',");
                    strSql1.Append("" + com.reviewer + ",");
                    strSql1.Append("" + com.branchreviewer + ",");
                    strSql1.Append("'" + com.qualityplan + "',");
                    strSql1.Append("'" + com.conferencereview + "',");
                    strSql1.Append("" + com.tp_id + "");
                    strSql1.Append(")");
                    command.CommandText = strSql1.ToString();
                    command.ExecuteNonQuery();
                }
                transaction.Commit();
                return 1;
            }
            catch
            {
                transaction.Rollback();
                return 0;
            }
            finally
            {
                transaction.Dispose();
                SQLHelper.myconn.Close();
            }
        }

        public int Update(TotalProject model, List<Commitment> list)
        {
            SQLHelper.myconn.Open();
            SqlCommand command = SQLHelper.myconn.CreateCommand();
            SqlTransaction transaction;
            transaction = SQLHelper.myconn.BeginTransaction();
            command.Connection = SQLHelper.myconn;
            command.Transaction = transaction;
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update pm_TotalProject set ");
                strSql.Append("tasktype='" + model.tasktype + "',");
                strSql.Append("stage='" + model.stage + "',");
                strSql.Append("taskno='" + model.taskno + "',");
                strSql.Append("projectno='" + model.projectno + "',");
                strSql.Append("commpany='" + model.commpany + "',");
                strSql.Append("name='" + model.name + "',");
                strSql.Append("leaders='" + model.leaders + "',");
                strSql.Append("projecttype='" + model.projecttype + "',");
                strSql.Append("progresstype='" + model.progresstype + "',");
                strSql.Append("person1='" + model.person1 + "',");
                strSql.Append("person2='" + model.person2 + "',");
                strSql.Append("person3='" + model.person3 + "',");
                strSql.Append("planner='" + model.planner + "',");
                strSql.Append("tpdate='" + model.tpdate + "',");
                strSql.Append("business='" + model.business + "',");
                strSql.Append("comments1='" + model.comments1 + "',");
                strSql.Append("comments2='" + model.comments2 + "',");
                strSql.Append("reviewedchange='" + model.reviewedchange + "',");
                strSql.Append("meetingchange='" + model.meetingchange + "',");
                strSql.Append("remark='" + model.remark + "',");
                strSql.Append("cooperationunit='" + model.cooperationunit + "',");
                strSql.Append("cooperationperson='" + model.cooperationperson + "'");
                strSql.Append(" where tp_id=" + model.tp_id + " ");
                command.CommandText = strSql.ToString();
                command.ExecuteNonQuery();
                string tpid = SQLHelper.ExecuteString("select max(tp_id) from pm_TotalProject", SQLHelper.myconn, transaction);
                command.CommandText = "delete from pm_tp_Commitment where tp_id='" + model.tp_id + "'";
                command.ExecuteNonQuery();
                foreach (Commitment com in list)
                {
                    StringBuilder strSql1 = new StringBuilder();
                    com.tp_id = int.Parse(tpid);
                    strSql1.Append("insert into pm_tp_Commitment(");
                    strSql1.Append("professional,departments,completiondate,deliverydate,reviewer,branchreviewer,qualityplan,conferencereview,tp_id");
                    strSql1.Append(")");
                    strSql1.Append(" values (");
                    strSql1.Append("'" + com.professional + "',");
                    strSql1.Append("" + com.departments + ",");
                    strSql1.Append("'" + com.completiondate + "',");
                    strSql1.Append("'" + com.deliverydate + "',");
                    strSql1.Append("" + com.reviewer + ",");
                    strSql1.Append("" + com.branchreviewer + ",");
                    strSql1.Append("'" + com.qualityplan + "',");
                    strSql1.Append("'" + com.conferencereview + "',");
                    strSql1.Append("" + com.tp_id + "");
                    strSql1.Append(")");
                    command.CommandText = strSql1.ToString();
                    command.ExecuteNonQuery();
                }
                transaction.Commit();
                return 1;
            }
            catch
            {
                transaction.Rollback();
                return 0;
            }
            finally
            {
                transaction.Dispose();
                SQLHelper.myconn.Close();
            }
        }

        public DataView getBaseData(string pid)
        {
            return SQLHelper.ExecuteDataView("select name,code from t_param where PCode='" + pid + "'");
        }

        public DataView getDept()
        {
            return SQLHelper.ExecuteDataView("select id,name from Sys_Org_Other_Info where ParentID<>0");
        }
    }
}

